export const strToVNode = (s) => {
  const div = document.createElement('div');
  div.innerHTML = s.trim();

  function eleToVNode(e) {
    // 注释节点
    if (e.nodeType === Node.COMMENT_NODE) {
      return {
        type: 'comment',
        content: e.textContent.trim()
      }
    }

    if (e.nodeType === Node.TEXT_NODE) {
      return {
        type: 'text',
        content: e.textContent.trim()
      };
    }

    const vnode = {
      tag: e.tagName.toLowerCase(),
      attrs: [],
      children: []
    };

    for (let i = 0; i < e.attributes.length; i++) {
      const attr = e.attributes[i];
      vnode.attrs.push({
        name: attr.name,
        value: attr.value
      });
    }

    for (let child = e.firstChild; child; child = child.nextSibling) {
      vnode.children.push(eleToVNode(child));
    }

    return vnode;
  }

  return eleToVNode(div.firstChild);
}
